import { Node } from '../engine/core/node.js';
import { Sprite } from '../engine/nodes/sprite.js';
import { vec3 } from '../engine/gl-matrix/index.js'
import { databus } from './databus.js'

export class Background extends Node {
    constructor() {
        super();

        this.scale = vec3.fromValues(databus.rightX, 1.0, 1.0);
        this.translation = vec3.fromValues(0.0, 0.0, 0.0);
        this.speed = -databus.backgroundSpeed;

        const sprite = new Sprite({ url: "images/bg.jpg" });
        sprite.material.renderOrder = 0;
        this.add(sprite);

        const sprite2 = new Sprite({ url: "images/bg.jpg" });
        sprite2.translation = vec3.fromValues(0, 2.0, 0);
        sprite2.material.renderOrder = 0;
        this.add(sprite2);
    }

    onUpdate(timestamp, frameDelta) {
        if(databus.gameOver) return;

        let y = this.translation[1];

        y += frameDelta * this.speed;

        if (y < -2) {
            y %= -2;
        }

        this.translation[1] = y;
    }
}